/******************************************************************
 * ParseHTML.cpp
 *
 *  ParseHTML: Lightweight UI-less HTML parser using MSHTML
 *
 *  Note: This is for accessing the DOM only. No image download, 
 *        script execution, etc...
 *
 *  8 June 2001 - Asher Kobin (asherk@pobox.com)
 *  
 *  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
 *  OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
 *  LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
 *  FITNESS FOR A PARTICULAR PURPOSE.
 *
 *******************************************************************/
 
#include <windows.h>
#include <mshtml.h>
 
OLECHAR szHTML[] = OLESTR("<HTML><BODY>Hello World!</BODY></HTML>");
 
int __stdcall WinMain(HINSTANCE hInst, 
                      HINSTANCE hPrev, 
                      LPSTR lpCmdLine, 
                      int nShowCmd)
{
  IHTMLDocument2 *pDoc = NULL;
 
  CoInitialize(NULL);
 
  CoCreateInstance(CLSID_HTMLDocument, 
                   NULL, 
                   CLSCTX_INPROC_SERVER, 
                   IID_IHTMLDocument2, 
                   (LPVOID *) &pDoc);
 
  if (pDoc)
  {
    IPersistStreamInit *pPersist = NULL;
 
    pDoc->QueryInterface(IID_IPersistStreamInit, 
                       (LPVOID *) &pPersist);
 
    if (pPersist)
    {
      IMarkupServices *pMS = NULL;
  
      pPersist->InitNew();
      pPersist->Release();
 
      pDoc->QueryInterface(IID_IMarkupServices, 
                              (LPVOID *) &pMS);
 
      if (pMS)
      {
        IMarkupContainer *pMC = NULL;
        IMarkupPointer *pMkStart = NULL;
        IMarkupPointer *pMkFinish = NULL;
 
        pMS->CreateMarkupPointer(&pMkStart);
        pMS->CreateMarkupPointer(&pMkFinish);
 
        pMS->ParseString(szHTML, 
                         0, 
                         &pMC, 
                         pMkStart, 
                         pMkFinish);
 
        if (pMC)
        {
          IHTMLDocument2 *pNewDoc = NULL;
 
          pMC->QueryInterface(IID_IHTMLDocument, 
                              (LPVOID *) &pNewDoc);
 
          if (pNewDoc)
          {
            // do anything with pNewDoc, in this case 
            // get the body innerText.
 
            IHTMLElement *pBody;
            pNewDoc-gt;get_body(&pBody);
 
            if (pBody)
            {
              BSTR strText;
 
              pBody->get_innerText(&strText);
              pBody->Release();
 
              SysFreeString(strText);
            }
 
            pNewDoc->Release();
          }
 
          pMC->Release();
        }
 
        if (pMkStart)
            pMkStart->Release();
 
        if (pMkFinish)
          pMkFinish->Release();
 
        pMS->Release();
      }
    }
 
    pDoc->Release();
  }
 
  CoUninitialize();
  
  return TRUE;
}